package com.wangdao.wechatmall.service.admin;

import com.github.pagehelper.PageInfo;
import com.wangdao.wechatmall.bean.bo.admin.BrandBO;
import com.wangdao.wechatmall.bean.bo.admin.BrandUpdateBO;
import com.wangdao.wechatmall.bean.bo.admin.CreateParamBO;
import com.wangdao.wechatmall.bean.pojo.Brand;
import com.wangdao.wechatmall.bean.pojo.BrandExample;
import com.wangdao.wechatmall.bean.vo.admin.BrandVO;
import com.wangdao.wechatmall.bean.vo.admin.CreateBrandVO;
import com.wangdao.wechatmall.exception.BrandDescException;
import com.wangdao.wechatmall.exception.BrandNameException;
import com.wangdao.wechatmall.exception.PriceException;
import com.wangdao.wechatmall.mapper.BrandMapper;
import com.wangdao.wechatmall.util.MyPageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author liyongqiang
 * CreateDate 2021/7/9 11:00
 */

@Service
public class BrandServiceImpl implements BrandService{

    @Autowired
    BrandMapper brandMapper;

    @Override
    public BrandVO queryList(BrandBO brandBO) {
        BrandVO brandVO = new BrandVO();
        MyPageHelper.startPage(brandBO);
        BrandExample brandExample = new BrandExample();
        BrandExample.Criteria criteria = brandExample.createCriteria();
        if (brandBO.getId() != null){
            criteria.andIdEqualTo(brandBO.getId());
        }
        if (brandBO.getName() != null){
            criteria.andNameLike("%" + brandBO.getName() + "%");
        }
        criteria.andDeletedEqualTo(0);
        brandExample.setOrderByClause(brandBO.getSort() + " " + brandBO.getOrder());

        List<Brand> brands = brandMapper.selectByExample(brandExample);

        PageInfo<Brand> pageInfo = new PageInfo<>();
        int total = (int) pageInfo.getTotal();
        brandVO.setItems(brands);
        brandVO.setTotal(total);
        return brandVO;
    }

    @Override
    public int delete(Integer id) {
        Brand brand = new Brand();
        brand.setId(id);
        brand.setDeleted(1);
        int delete = brandMapper.updateByPrimaryKeySelective(brand);
        return delete;

    }

    @Override
    public int update(BrandUpdateBO brand) throws BrandNameException, BrandDescException {

        if (brand.getName().length() > 50){
            throw new BrandNameException();
        }
        if (brand.getDesc().length() > 1000){
            throw new BrandDescException();
        }
        Brand brand1 = new Brand();
        brand1.setId(brand.getId());
        brand1.setName(brand.getName());
        brand1.setDesc(brand.getDesc());
        brand1.setPicUrl(brand.getPicUrl());
        brand1.setSortOrder(brand.getSortOrder());
        brand1.setFloorPrice(Double.parseDouble(brand.getFloorPrice()));
        brand1.setAddTime(brand.getAddTime());
        brand1.setUpdateTime(brand.getUpdateTime());
        brand1.setDeleted(brand.getDeleted());

        int update = brandMapper.updateByPrimaryKeySelective(brand1);
        return update;
    }

    @Override
    public CreateBrandVO create(CreateParamBO createParamBO) throws PriceException, BrandNameException, BrandDescException {
        // 用正则表达式判断floorPrice是否符合规范
//        String pattern = "(\\d+(\\.\\d+)?)";
//        if (!createParamBO.getFloorPrice().matches(pattern) && !createParamBO.getFloorPrice().equals("")){
//            throw new PriceException();
//        }
        if (createParamBO.getName().length() > 50){
            throw new BrandNameException();
        }
        if (createParamBO.getDesc().length() > 1000){
            throw new BrandDescException();
        }
        Brand brand = new Brand();
        brand.setName(createParamBO.getName());
        brand.setDesc(createParamBO.getDesc());
        brand.setPicUrl(createParamBO.getPicUrl());
        brand.setFloorPrice(Double.parseDouble(createParamBO.getFloorPrice()));
        brand.setAddTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        brand.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        brand.setDeleted(0);
        int i = brandMapper.insertSelective(brand);
        Brand brand1 = brandMapper.selectByPrimaryKey(brand.getId());
        CreateBrandVO createBrandVO = new CreateBrandVO(brand1.getId(), brand1.getName(), brand1.getDesc(), brand1.getPicUrl(),brand1.getFloorPrice(), brand1.getAddTime(), brand1.getUpdateTime());

        return createBrandVO;
    }
}
